package com.lsp.server.user.controller;

import com.lsp.server.common.Apiresponse;
import com.lsp.server.user.domain.po.UserEntity;
import com.lsp.server.user.service.UserService;
import com.lsp.server.utils.FileUploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @ClassName UserController
 * @Description 用户管理控制层
 * @Author Mr Hao
 * @Date 2021/11/14 18:02
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     *  获取用户列表
     * @return
     */
    @PostMapping("/list")
    public Apiresponse getUser(){
        try{
            return Apiresponse.success(userService.getUser());
        }catch (Exception e){
            return Apiresponse.Error(500,"查询失败");
        }
    }

    @PostMapping("/userExistsByName")
    public Apiresponse userExistsByName(String name){
        if (StringUtils.isEmpty(name)){
            return Apiresponse.Error(400,"参数错误");
        }
        return Apiresponse.success(userService.userExistsByName(name));
    }

    /**
     * 添加新用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Apiresponse addUser(UserEntity user){
            if(null == user.getName()){
                return Apiresponse.Error(400,"参数错误");
            }
            userService.addUser(user);
            return Apiresponse.success("新增成功");
    }

    /**
     *
     * @param img
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadFile")
    public Apiresponse uploadFile(MultipartFile img)throws Exception{
        String fileType = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
        return Apiresponse.success(FileUploadUtils.uploadFileOss(img.getInputStream(),"imgs/"+UUID.randomUUID().toString()+fileType));
    }

    /**
     * 修改用户
     * @param form
     * @return
     */
    @PostMapping("/update")
    public Apiresponse updataUser(UserEntity form){
        try {
            if (null == form){
                return Apiresponse.Error(400,"参数错误");
            }
            userService.upateUesr(form);
            return Apiresponse.success("修改成功");
        }catch (Exception e){
            return Apiresponse.Error(500,"修改失败");
        }
    }

    /**
     * 根据id回显用户信息
     * @param id
     * @return
     */
    @PostMapping("findUserById")
    public Apiresponse findUserById(String id){
        try{
            if (null == id){
                return Apiresponse.Error(400,"参数错误");
            }
            return Apiresponse.success(userService.findUserById(id));
        }catch (Exception e){
            return Apiresponse.Error(500,e.getMessage());
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public Apiresponse delUserById(String ids){
        try {
            if (null == ids){
                return Apiresponse.Error(400,"获取id失败");
            }
            userService.delUserById(ids);
            return Apiresponse.success("删除成功");
        }catch (Exception e){
            return Apiresponse.Error(500,e.getMessage());
        }
    }
}
